package com.fbs.wowo.blockchain.service.imp;

import com.fbs.wowo.attest.entity.Attest;
import com.fbs.wowo.attest.mapper.AttestMapper;
import com.fbs.wowo.blockchain.service.BrowserService;
import com.fbs.wowo.blockchain.vo.BlockDetail;
import com.fbs.wowo.blockchain.vo.BlockchainStatistics;
import com.fbs.wowo.correctRight.entity.CorrectRight;
import com.fbs.wowo.correctRight.mapper.CorrectRightMapper;
import com.fbs.wowo.essay.entity.Essay;
import com.fbs.wowo.essay.mapper.EssayMapper;
import com.fbs.wowo.exception.NoSuchDataException;
import com.fbs.wowo.manage.mapper.UserMapper;
import com.fbs.wowo.util.dbutils.MongoUtils;
import com.fbs.wowo.util.eos.EOSService;
import com.routz.eos.rpctool.domain.Block;
import com.routz.eos.rpctool.domain.ChainInfo;
import com.routz.eos.rpctool.service.EosService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("browserService")
@Transactional
public class BrowserServiceImp implements BrowserService {

    /**
     * 搜索框
     */
    public Object search(String search) throws IOException {
        // 长度 12 账户名
//        if (search.length() == 12) {
//            return null;
//        }
        Map<String, Object> map = new HashMap<>();
        // 长度
        if (search.length() == 64) {
            BlockDetail detail = detail(search);
            map.put("data", detail);
            map.put("type", "transaction");
            return map;
        }

        // block id
        Block block = EosService.getBlock(search);
        map.put("data", block);
        map.put("type", "block");
        return map;
    }
    /**
     * 区块高度
     * 交易数量
     * 用户数量
     * @return BlockchainStatistics
     */
    @Override
    public BlockchainStatistics statistics() {
        BlockchainStatistics stat = new BlockchainStatistics();
        ChainInfo chainInfo = eosService.getChainInfo();
        stat.setBlockNum(chainInfo.getHeadBlockNum());
        Integer count = userMapper.getCount();
        stat.setUserNum("" + count);
        stat.setTransNum("312459378");
        return stat;
    }

    /**
     * 交易记录
     * @param pageNo 页码
     * @param pageSize 每页数据
     * @return list
     */
    @Override
    public List findTransactions(Integer pageNo, Integer pageSize) {
        List list = MongoUtils.find("transactions", pageNo, pageSize);
        return list;
    }

    /**
     * 单条交易记录
     * @param trxId
     * @return
     */
    @Override
    public Object findTransaction(String trxId) {
        return MongoUtils.findOne("transactions", "trx_id", trxId);
    }

    /**
     * 区块信息
     *
     * @param pageNo   页码
     * @param pageSize 每页数据
     * @return list
     */
    @Override
    public List findBlocks(Integer pageNo, Integer pageSize) {
        return MongoUtils.find("block_states", pageNo, pageSize);
    }

    @Override
    public BlockDetail detail(String transactionId) throws NoSuchDataException {
        BlockDetail bd = new BlockDetail();
        // 1 存证 2 确权
        Attest attest = attestMapper.findAttestByTransId(transactionId);
        if (attest != null) {
            Essay essay = essayMapper.selectByPrimaryKey(attest.getEssayId());

            bd.setConfirmed(new Integer(essay.getConfirmed()));
            bd.setProductType(essay.getType() + "");
            bd.setProductId(attest.getId());
            bd.setAuthor(null);
            bd.setOwnerId(attest.getUserId());
            bd.setBlockNum(attest.getBlockHeight());
            bd.setHash(attest.getHashValue());
            bd.setTitle(attest.getTitle());
            bd.setTransactionId(attest.getRegisterId());
            bd.setTransTime(attest.getCreateDate());

        } else {
            CorrectRight cr = correctRightMapper.findCorrectRightByTransId(transactionId);
            if (cr != null) {
                Essay essay = essayMapper.selectByPrimaryKey(cr.getEssayId());
                if(essay != null) {

                    bd.setConfirmed(new Integer(essay.getConfirmed()));
                    bd.setProductType(essay.getType() + "");
                    bd.setProductId(essay.getId());
                    bd.setAuthor(cr.getAuthor());
                    bd.setOwnerId(cr.getBelonger());
                    bd.setBlockNum(cr.getBlockHeight());
                    bd.setHash(essay.getHashValue());
                    bd.setTitle(essay.getTitle());
                    bd.setTransactionId(cr.getRegisterId());
                    bd.setTransTime(cr.getCreateDate());
                }else{
//                    throw new NoSuchDataException();
                    return null;
                }
            } else {
//                throw new NoSuchDataException();
                return null;
            }
        }

        return bd;
    }

    @Autowired
    private EOSService eosService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AttestMapper attestMapper;

    @Autowired
    private CorrectRightMapper correctRightMapper;

    @Autowired
    private EssayMapper essayMapper;

}
